package com.ywz.framework.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

import static java.util.Collections.singletonList;


/**
 * 类描述 -> Swagger配置类
 *
 * @Author: ywz
 * @Date: 2024/09/27
 */
@EnableOpenApi
@Configuration
public class SwaggerConfig {
    @Value("${swagger.enable}")
    private boolean enable;

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                // 是否开启swagger
                .enable(enable)
                .select()
                .apis(RequestHandlerSelectors.any())
                .build().globalRequestParameters(
                        singletonList(new springfox.documentation.builders.RequestParameterBuilder()
                                // 不能叫Authorization
                                .name("token")
                                .description("token")
                                .in(ParameterType.HEADER)
                                .required(false)
                                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                                .build()));

    }

    private ApiInfo apiInfo() {
        /*作者信息*/
        Contact contact = new Contact("杨柳", "https://youwangzi.blog.csdn.net", "280327315@qq.com");
        return new ApiInfo(
                "游王子og管理平台",
                "Spring Boot 集成 Swagger3 接口文档",
                "v1.0",
                "https://youwangzi.blog.csdn.net",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>()
        );
    }
}
